/*
 * Created by JFormDesigner on Mon Mar 11 14:42:24 CST 2019
 */

package com.n9.qq.util;

import com.n9.qq.android.mock.VfCodeHandler;
import com.n9.qq.android.mock.VfImg;

import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Objects;

/**
 * @author Brainrain
 */
public class VFCodeForm extends JFrame implements ActionListener, VfCodeHandler {

    private static final VFCodeForm me = new VFCodeForm();

    public static VFCodeForm instance() {
        return me;
    }

    private String getVFCode0(Object obj) {
        if (Objects.isNull(obj)) {
            return null;
        }

        synchronized (VFCodeForm.class) {
            JTextField tf = me.vfCodeTextField;

            me.setVisible(true);
            tf.setText("");
            try {
                if (obj instanceof String) {
                    me.vfCodeImg.setText(obj.toString());
                } else {
                    me.vfCodeImg.setIcon(new ImageIcon((byte[])obj));
                }
                ThreadUtils.wait(VFCodeForm.class);
            } catch (Exception e) {
                me.vfCodeImg.setText("图片数据有误");
            }
            String vfCode = tf.getText().trim();
            tf.setText("");
            me.vfCodeImg.setIcon(null);
            me.vfCodeImg.setText(null);

            me.setVisible(false);
            return vfCode;
        }
    }

    @Override
    public String process(VfImg img) {
        return getVFCode0(img.getImgData());
    }

    @Override
    public String process(String phoneNumber) {
        return getVFCode0(phoneNumber);
    }

    private VFCodeForm() {
        initComponents();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        vfCodeImg = new JLabel();
        panel1 = new JPanel();
        vfCodeTextField = new JTextField();
        button1 = new JButton();

        //======== this ========
        setTitle("\u9a8c\u8bc1\u7801");
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //---- vfCodeImg ----
        vfCodeImg.setPreferredSize(new Dimension(200, 100));
        vfCodeImg.setBorder(LineBorder.createBlackLineBorder());
        contentPane.add(vfCodeImg, BorderLayout.CENTER);

        //======== panel1 ========
        {
            panel1.setPreferredSize(new Dimension(210, 60));
            panel1.setLayout(new FlowLayout());

            //---- vfCodeTextField ----
            vfCodeTextField.setPreferredSize(new Dimension(120, 45));
            panel1.add(vfCodeTextField);

            //---- button1 ----
            button1.setText("\u63d0\u4ea4");
            button1.setPreferredSize(new Dimension(80, 45));
            panel1.add(button1);
        }
        contentPane.add(panel1, BorderLayout.SOUTH);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
        this.setResizable(false);
        button1.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        ThreadUtils.notifyAll(VFCodeForm.class);
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel vfCodeImg;
    private JPanel panel1;
    private JTextField vfCodeTextField;
    private JButton button1;


    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
